package dyyx;

import java.io.BufferedWriter;
import java.io.File;
import java.util.Date;
import java.util.Random;

import dyyx.util.CommUtil;

public class DataGen {

	private static final Random RANDOM = new Random();

	private static final int ROWS = CommUtil.getIntFromSystemProp("rows", 100000);

	private static final int ACCOUNT_COUNT = CommUtil.getIntFromSystemProp("account", 1000);
	private static final int IP_COUNT = CommUtil.getIntFromSystemProp("ip", 1000);
	private static final int DEVICE_COUNT = CommUtil.getIntFromSystemProp("device", 1000);

	private static final long SECOND_ONE = 1000L;
	private static final long MINUTE_ONE = 60L * SECOND_ONE;
	private static final long HOUR_ONE = 60L * MINUTE_ONE;
	private static final long DAY_ONE = 24L * HOUR_ONE;

	private static final long MAX_TIME_MINUS = 365L * DAY_ONE;
	private static final int MAX_TIME_MINUS_SECOND = (int) (MAX_TIME_MINUS / 1000);

	private static final String file = System.getProperty("file");

	public static void main(String[] args) throws Exception {
		// System.out.println("hello,clickhouse,"+new Date());

		if (CommUtil.isBlank(file)) {
			System.out.println("file blank");
			return;
		}
		System.out.println("ROWS=" + ROWS);
		System.out.println("ACCOUNT_COUNT=" + ACCOUNT_COUNT);
		System.out.println("IP_COUNT=" + IP_COUNT);
		System.out.println("DEVICE_COUNT=" + DEVICE_COUNT);
		System.out.println("file=" + file);
		
		File f = new File(file);
		if(f.exists()){
			System.out.println("file exist");
			return;
		}

		BufferedWriter bw = null;

		long start = System.currentTimeMillis();

		try {
			bw = CommUtil.createBufferedWriter(file, null);

			for (int i = 0; i < ROWS; i++) {
				// System.out.println( genData());
				bw.write(genData().toString());
				bw.newLine();

				if (i > 0 && (i % 10000) == 0) {
					long end = System.currentTimeMillis();
					long time = end - start;
					System.out.println("write rows " + i + ",time=" + time);
				}

			}
		} finally {
			CommUtil.close(bw);
		}
		long end = System.currentTimeMillis();
		long time = end - start;
		System.out.println("data gen done,rows=" + ROWS+",time="+time+",file="+file);
	
		// System.out.println("total time " + time);

	}

	public static Data genData() {
		String account = genAccount();
		String account_recv = genAccount();
		Date tradeTime = genTradeTime();

		Data data = new Data();

		data.account = account;
		data.account_recv = account_recv;
		data.trade_date = CommUtil.getDateString(tradeTime);
		data.trade_time = CommUtil.getDateTimeString(tradeTime);
		data.amount = genAmount();
		data.ip = genIp();
		data.device = genDevice();

		return data;
	}

	private static String genAccount() {
		return "account_" + RANDOM.nextInt(ACCOUNT_COUNT);
	}

	public static Date genTradeTime() {
		long now = System.currentTimeMillis();

		return new Date(now - RANDOM.nextInt(MAX_TIME_MINUS_SECOND) * 1000L);
	}

	private static int genAmount() {
		return 100 + RANDOM.nextInt(10000);
	}

	private static String genIp() {
		return "ip_" + RANDOM.nextInt(IP_COUNT);
	}

	private static String genDevice() {
		return "device_" + RANDOM.nextInt(DEVICE_COUNT);
	}

}
